﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace DAL
{
    public class PaymentDAL
    {
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetAllPayment()
        {
            return DbHelperSQL.Query(@"select 
                                        ExamDB.dbo.tb_Activity_Payment.activity_id,
                                        payment_date,
                                        payment_count,
                                        payment_money,
                                        activity_name 
                                        from ExamDB.dbo.tb_Activity_Payment,ExamDB.dbo.tb_Activity 
            where ExamDB.dbo.tb_Activity_Payment.activity_id=ExamDB.dbo.tb_Activity.activity_id");
        }

        public static DataSet GetPaymentByUID(string userID)
        {
            if (string.IsNullOrEmpty(userID))
            {
                return null;
            }
            else
            {
                DataSet ds = new DataSet();
                StringBuilder strSql = new StringBuilder();
                strSql.Append(@"select 
                                        ExamDB.dbo.tb_Activity_Payment.activity_id,
                                        payment_date,
                                        payment_count,
                                        payment_money,
                                        activity_name 
                                        from ExamDB.dbo.tb_Activity_Payment,ExamDB.dbo.tb_Activity 
                where ExamDB.dbo.tb_Activity_Payment.activity_id=ExamDB.dbo.tb_Activity.activity_id");
                strSql.Append(" and ExamDB.dbo.tb_Activity.activity_enterpriseuid='" + userID + "'");
                ds = DbHelperSQL.Query(strSql.ToString());
                return ds;
            }
        }

        /// <summary>
        /// 续费
        /// </summary>
        /// <param name="activity_id">活动ID</param>
        /// <param name="payment_money">交费金额</param>
        /// <param name="payment_count">交费次数</param>
        /// <returns></returns>
        public static int AddPayment(int activity_id, float payment_money, int payment_count)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ExamDB.dbo.tb_Activity_Payment(");
            strSql.Append(@"activity_id,
                            payment_date,
                            payment_count,
                           payment_money)");

            strSql.Append(" values (");

            strSql.Append(@"@activity_id,
                            @payment_date,
                            @payment_count,
                            @payment_money)");

            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@activity_id", SqlDbType.Int,4),
					new SqlParameter("@payment_date", SqlDbType.DateTime ),
					new SqlParameter("@payment_count", SqlDbType.Int,4),
					new SqlParameter("@payment_money", SqlDbType.Float)
                                        };
            parameters[0].Value = activity_id;
            parameters[1].Value = DateTime.Now;
            parameters[2].Value = payment_count;
            parameters[3].Value = payment_money;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return -1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
